package com.ruoyi.web.controller.applets;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.task.domain.TaskReport;
import com.ruoyi.task.service.ITaskReportService;
import com.ruoyi.task.service.ITaskService;
import com.ruoyi.wechat.domain.WechatUser;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.ruoyi.common.constant.HttpStatus.LOGOUT;

/**
 * 小程序任务Controller
 *
 * @author KevenPotter
 * @date 2024-05-02 19:05:27
 */
@RestController
@RequestMapping("/applets/task/report")
@AllArgsConstructor
public class AppletsTaskReportController extends BaseController {

    private final ITaskService taskService;
    private final ITaskReportService taskReportService;
    private final TokenService tokenService;

    /**
     * 新增任务举报
     *
     * @param taskReport 任务举报对象
     * @return 返回是否新增成功
     * @author KevenPotter
     * @date 2023-05-02 19:06:13
     */
    @Anonymous
    @PostMapping("/add")
    public AjaxResult add(@RequestBody TaskReport taskReport) {
        if (tokenService.checkAppletsToken()) {
            WechatUser loginWechatUser = tokenService.getLoginWechatUser();
            taskReport.setReportUserId(loginWechatUser.getId());
            taskReport.setTaskTitle(taskService.getById(taskReport.getTaskId()).getTitle());
            taskReport = taskReportService.filterInbound(taskReport, null);
            return toAjax(taskReportService.insertTaskReport(taskReport));
        }
        return AjaxResult.error(LOGOUT, "请重新登录");
    }

}
